<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {
    private $total=0;

	public function index()
	{  
        $cat = $this->common_model->getdata('product_category');
        foreach ($cat as $cat2){
            $mid[$cat2->id]=$cat2;
            $mcat[$cat2->parentid][]=$cat2;
        }
        // 1:addproduct: add san pham cho tung danh muc
        //2 cap nhat day du
        $this->addproduct();
	 	//$this->capnhatdaydu();
        
	}
    function addproduct(){// dem tong so san pham cua tung danh muc
        $data = $this->common_model->getdata('product_category','',array('id','parentid','total'));
        if(!empty($data)){
            foreach ($data as $data1){
                $this->db->select(array('id'));
                 $data2 = $this->common_model->getnumber2('product',array('category'=>$data1->id));
                 $data1->total=$data2;
                 $this->db->where('id',$data1->id);
                 $this->db->update('product_category',$data1);
            }
        }
    }
    function capnhatdaydu(){// ghi lai day du tong so san pham
    
        $data = $this->common_model->getdata('product_category','',array('id','parentid','total'));
        foreach ($data as $cat2){
            $mid[$cat2->id]=$cat2;
            $mcat[$cat2->parentid][]=$cat2;
        }
        if(!empty($data)){
            foreach ($data as $data1){
                 $this->total = 0;
                 $this->get_child_cat($data1->id,$mcat);
                 $data1->total=$this->total;
                 $this->db->where('id',$data1->id);
                 $this->db->update('product_category',$data1);
            }
        }
    }
    function get_child_cat($id=0,$mcat){// lay danh muc con nho nhat 
        if(!empty($mcat[$id])){
           foreach ($mcat[$id] as $child){//nhieu menu con
           $this->total = $this->total+ $child->total ;
            if(!empty($mcat[$child->id])){
                $this->get_child_cat($child->id,$mcat);
            }
            } 
        }else   $this->total = $this->common_model->getnumber2('product',array('category'=>$id)); ;      
        
    }
    function add_total($cat){// dem tong so san pham add vao menu
       
    }
   
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */